package com.gitee.huanminabc.utils_tools.security.dal.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.huanminabc.nullchain.Null;
import com.gitee.huanminabc.dynamicdb.aop.DBSwitch;
import com.gitee.huanminabc.utils_tools.security.dal.dao.UserDaoService;
import com.gitee.huanminabc.utils_tools.security.dal.mapper.UserMapper;
import com.gitee.huanminabc.utils_tools.security.entity.UserEntity;
import com.gitee.huanminabc.utils_tools.security.enums.UserStatusEnum;
import com.gitee.huanminabc.utils_tools.security.param.UserUpdateParam;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@DBSwitch("resource")
public class  UserDaoServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements UserDaoService {
    @Override
    public UserEntity findByUser(String account, String password) {
        LambdaQueryWrapper<UserEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserEntity::getAccount, account);
        queryWrapper.eq(UserEntity::getPassword, password);
        return getOne(queryWrapper);
    }

    @Override
    public UserEntity findByUser(String account) {
        LambdaQueryWrapper<UserEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserEntity::getAccount, account);
        queryWrapper.eq(UserEntity::getStatus, UserStatusEnum.NORMAL);
        return getOne(queryWrapper);
    }

    @Override
    public List<UserEntity> findByUsers(List<String> accounts) {
        LambdaQueryWrapper<UserEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(UserEntity::getAccount, accounts);
        return list(queryWrapper);
    }

    @Override
    public UserEntity findByPhone(String phone) {
        LambdaQueryWrapper<UserEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserEntity::getPhoneNumber, phone);
        return getOne(queryWrapper);
    }

    @Override
    public int insertUser(UserEntity userEntity) {
        userEntity.setCreateTime(new Date());
        userEntity.setUpdateTime(new Date());
        return baseMapper.insert(userEntity);
    }

    @Override
    public List<UserEntity> getNotLikeRightUserList(String account) {
        LambdaQueryWrapper<UserEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.notLikeRight(UserEntity::getAccount, account);
        return list(queryWrapper);
    }

    @Override
    public boolean updateUser(UserUpdateParam userUpdateParam) {
        UserEntity userEntity = new UserEntity();

        //检查id
        if (userUpdateParam.getId() == null) {
            throw new RuntimeException("UserDaoServiceImpl:::updateUser->id不能为空");
        }
        userEntity.setId(userUpdateParam.getId());


        Null.of(userUpdateParam.getNickName()).ifPresent(userEntity::setNickName);

        Null.of(userUpdateParam.getEmail()).ifPresent(userEntity::setEmail);

        Null.of(userUpdateParam.getSex()).ifPresent(userEntity::setSex);

        Null.of(userUpdateParam.getEducation()).ifPresent(userEntity::setEducation);

        Null.of(userUpdateParam.getStartWordTIme()).ifPresent(userEntity::setStartWordTIme);

        Null.of(userUpdateParam.getBirthdate()).ifPresent(userEntity::setBirthdate);

        Null.of(userUpdateParam.getLocation()).ifPresent(userEntity::setLocation);

        Null.of(userUpdateParam.getDesc()).ifPresent(userEntity::setDesc);

        userEntity.setUpdateTime(new Date());
        return updateById(userEntity); //只保证不是null的进行修改, 如果是空字符串那么也会进行修改,所以需要进行判断
    }

    @Override
    public List<UserEntity> findByUserName(String name) {
        LambdaQueryWrapper<UserEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserEntity::getNickName, name);
        return list(queryWrapper);
    }


}
